<?php

/**
This script handles the effects in the signup page of the site.
*/

/*
These are argument constants for the functions in this file. To call
an argument, say, to getDescription for a University-Level user, use
$USERS[$UNIVERSITY]

TODO: Make the argument style ala signup_effects.js
*/
$USERS = array("university", "college", "department", "instructor");
/*$UNIVERSITY = 0;
$COLLEGE = 1;
$DEPARTMENT = 2;
$INSTRUCTOR = 3;*/

$JAVASCRIPT_OPENER = "<script type='text/javascript' language='javascript'>";
$JAVASCRIPT_CLOSER = "</script>";

/*
Gets the description file for a user. Note that description files follow
the form

	desc_<user level name>.txt

for a filename.

Descriptions should be in a single line only on the file (better turn line
wrapping on).

@param $forUser - An integer signifying a user level. Use the constants
defined above.
@return The whole textual description for a user.
*/
function getDescription($forUser){
	global $USERS;
	$filename = "desc_" . $USERS[$forUser] . ".txt";
	$description_file = fopen($filename, "r");
	$description = fread($description_file, filesize($filename));
	fclose($description_file);
	return $description;
}

/*
Returns a description for the given user level.
*/
function loadDescription($forUser){
	return "<p>" . getDescription($forUser) . "</p>";
}

/*
Gets the HTML codes for a sign-up form for a specific user from a file.
Filenames should follow the form:

	form_<user level name>.txt

TODO: Handle empty lines, whitespace lines, etc.

@param $forUser - An integer signifying a user level. Use the constants
defined above.
@return An array containing the HTML codes for a form.
*/
function getForm($forUser){
	global $USERS;
	$form_file = fopen("form_" . $USERS[$forUser] . ".txt", "r");
	$theForm = array();
	
	while(!feof($form_file)){
		$sanitized = str_replace(array("\r\n", "\r", "\n"), "", fgets($form_file));
		array_push($theForm, $sanitized);
	}
	
	return $theForm;
}

/*
Echoes the form for a user.
*/
function loadForm($forUser){
	$form = getForm($forUser);
	$i = 0;
	$limit = count($form);
	
	while($i < $limit){
		echo $form[$i];
		$i++;
	}
}

function makeDescriptionsArray(){
	global $USERS, $JAVASCRIPT_OPENER, $JAVASCRIPT_CLOSER;
	echo $JAVASCRIPT_OPENER;
	echo "var DESCRIPTIONS = new Array()\n";
	$i = 0;
	$limit = count($USERS);
	
	while($i < $limit){
		echo "DESCRIPTIONS[$i] = '" . loadDescription($i) . "'\n";
		$i++;
	}
	
	echo $JAVASCRIPT_CLOSER;
}

/*
Note to self: Since we are creating the JavaScript arrays in PHP, should
the need arise to load form contents from a database, it must be easy to
allow and parse for "special inputs" which will signal us to load from a
database. After loading from a database, we insert them into the JavaScript
array.
*/
function toBigString($phpArray){
	$i = 0;
	$limit = count($phpArray);
	$BigString = "'";
	
	while($i < $limit){
		$BigString .= $phpArray[$i];
		$i++;
	}
	
	$BigString .= "'";
	
	return $BigString;
}

function makeFormsArray(){
	global $USERS, $JAVASCRIPT_OPENER, $JAVASCRIPT_CLOSER;
	echo $JAVASCRIPT_OPENER;
	echo "var FORMS = new Array()\n";
	$i = 0;
	$limit = count($USERS);
	
	while($i < $limit){
		$form_contents = getForm($i);
		echo "FORMS[$i] = " . toBigString($form_contents) . "\n";
		$i++;
	}
	
	echo $JAVASCRIPT_CLOSER;
}